﻿using System;
using System.ComponentModel;
#if CONTRACTS_FULL
using System.Diagnostics.Contracts;
#endif

namespace Elca.MvvmHelpers {

    public abstract class BaseNotifyableObject : INotifyPropertyChanged {
        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        /// <summary>
        /// This can be used to indicate that all property values have changed.
        /// </summary>
        protected void OnPropertyChanged() {
            OnPropertyChanged(null);
        }

        /// <summary>
        /// This raises the INotifyPropertyChanged.PropertyChanged event to indicate
        /// a specific property has changed value.
        /// </summary>
        /// <param name="name"></param>
        protected virtual void OnPropertyChanged(string name) {

#if CONTRACTS_FULL
            Contract.Requires(string.IsNullOrEmpty(name) || GetType().GetProperty(name) != null);
#endif
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        /// <summary>
        /// This raise the property changed notification for a group of property names
        /// </summary>
        /// <param name="propertyNames"></param>
        protected void OnPropertiesChanged(params string[] propertyNames) {
            if (propertyNames != null) {
                foreach (var name in propertyNames)
                    OnPropertyChanged(name);
            } else
                OnPropertyChanged(null);
        }
    }
}
